package com.ztjava.weixin.service;

import java.util.Map;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.client.Client;
import com.ztjava.plugin.weixin.WeixinKit;
import com.ztjava.plugin.weixin.WeixinSdk;
import com.ztjava.weixin.model.scene.SceneForever;
import com.ztjava.weixin.model.scene.SceneRecord;

public class SceneService extends BaseService{

	//新建场景
	public Ret insertForever(String name,String plugin,String component,String model,Integer object_id,String event,String is_record,Integer user_id){
		SceneForever sceneForever = new SceneForever();
		sceneForever.set("name", name);
		sceneForever.set("plugin", plugin);
		sceneForever.set("component", component);
		sceneForever.set("model", model);
		sceneForever.set("object_id",object_id);
		sceneForever.set("event", event);
		sceneForever.set("is_record", is_record);
		sceneForever.set("create_user", user_id);
		sceneForever.save();
		WeixinSdk weixinScene = WeixinKit.use();
		//很有趣，limit标识永久。。。
		Ret result = weixinScene.qrcode_create("QR_LIMIT_SCENE", sceneForever.getInt("id"), 0);
		if(result.getStr(Constant.RESULT).equals(Constant.SUCCESS)){
			sceneForever.set("ticket", result.getStr("ticket"));
			sceneForever.set("url", result.getStr("url"));
			sceneForever.update();
			return Ret.create(Constant.RESULT, Constant.SUCCESS).set("ticket", result.getStr("ticket")).set("url", result.getStr("url"));
		}
		return Ret.create(Constant.RESULT, Constant.ERROR).set("message",result.getStr(Constant.MESSAGE));
	}
	public Ret updateForeverUrl(SceneForever sceneForever){
		WeixinSdk weixinScene = WeixinKit.use();
		//很有趣，limit标识永久。。。
		Ret result = weixinScene.qrcode_create("QR_LIMIT_SCENE", sceneForever.getInt("id"), 0);
		if(result.getStr(Constant.RESULT).equals(Constant.SUCCESS)){
			sceneForever.set("ticket", result.getStr("ticket"));
			sceneForever.set("url", result.getStr("url"));
			sceneForever.update();
			return Ret.create(Constant.RESULT, Constant.SUCCESS).set("ticket", result.getStr("ticket")).set("url", result.getStr("url"));
		}
		return Ret.create(Constant.RESULT, Constant.ERROR).set("message",result.getStr(Constant.MESSAGE));
	}
	
	/*************
	 *Forever分页
	**************/
	public Page<SceneForever> getForeverPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
			new String[]{"plugin",eq,"plugin"},
			new String[]{"component",eq,"component"},
			new String[]{"model",eq,"model"},
			new String[]{"object_id",eq,"object_id"},	
			new String[]{"name",like,"name"},	
			new String[]{"is_record",eq,"is_record"},		
			new String[]{"enable_flag",eq,"enable_flag"}
		};
		Page<SceneForever> pd = SceneForever.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+SceneForever.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList()		, new String[]{"is_record","SHIFOU"}		
		, new String[]{"enable_flag","ENABLE_FLAG"});
		return pd;
	}
	
	public void insertRecord(Integer forever_id,String openid){
		SceneRecord sceneRecord = SceneRecord.dao.findFirst("select * from "+SceneRecord.tablename+" where openid=?",openid);
		if(sceneRecord==null){
			sceneRecord = new SceneRecord();
			sceneRecord.set("forever_id", forever_id);
			sceneRecord.set("openid", openid);
			sceneRecord.save();
		}
	}
	
	/*************
	 *Record分页
	**************/
	public Page<SceneRecord> getRecordPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
				new String[]{"start_time",gte,"A.create_time"},
				new String[]{"end_time",lte,"A.create_time"}
			};
		Page<SceneRecord> pd = SceneRecord.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select A.*,B.name","from "+SceneRecord.tablename+" A left join "+SceneForever.tablename+" B ON A.forever_id=B.id where 1=1 " 				
				+whereSql(paramMap, param)
				+ orderby("A.",client, paramMap),whereParam(paramMap, param));
		return pd;
	}

}
